package cn.example.vip.mall.seckill.service.impl;
import cn.example.vip.mall.seckill.entity.SeckillGoods;
import cn.example.vip.mall.seckill.entity.SeckillOrder;
import cn.example.vip.mall.seckill.mapper.SeckillGoodsMapper;
import cn.example.vip.mall.seckill.mapper.SeckillOrderMapper;
import cn.example.vip.mall.seckill.service.SeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.Map;

@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements SeckillOrderService {

    // 库存不足
    public static final Integer STORE_LACK = 0;
    // 抢单成功
    public static final Integer ADD_ORDER_OK = 1;
    
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;  // 抢单-数据库操作
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;  // 获取商品信息
    @Autowired
    private RedisTemplate redisTemplate;    // 获取缓存的秒杀商品信息
    
    /**
     * 秒杀商品抢单:1.热门,2.非热门
     * @param paramMap
     * @return
     */
    @Override
    public int addOrder(Map<String, Object> paramMap) {
        // username
        String username = paramMap.get("username").toString();
        // id
        String id = paramMap.get("id").toString();
        // num
        Integer num = Integer.parseInt(paramMap.get("num").toString());
        // 4.判断商品类型(热门/非热门)
        // 抢单操作: 1.判断库存余量,2.添加订单,3.库存减数量,4.完成抢单
        // 1
        Object storeCount = redisTemplate.boundHashOps("HotSeckillGoods").get(id);
        if(storeCount == null || Integer.parseInt(storeCount.toString()) < num){
            // 移除排队标识
//            redisTemplate.boundHashOps("HotSeckillGoods").delete(id);
            redisTemplate.delete("OrderQueue" + username);
            return STORE_LACK;
        }
        
        // 获取商品单价
        SeckillGoods seckillGoods = seckillGoodsMapper.selectById(id);
        
        // 2
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUsername(username);
        seckillOrder.setSeckillGoodsId(id);
        seckillOrder.setCreateTime(Date.from(Instant.now()));
        seckillOrder.setNum(num);
        seckillOrder.setMoney((double)num * seckillGoods.getPrice());
        seckillOrder.setStatus(0);
        seckillOrderMapper.insert(seckillOrder);
        
        // 3
        // Redis库存递减
        Long lastStoreCount = redisTemplate.boundHashOps("HotSeckillGoods").increment(id, -num);
        if(lastStoreCount <= 0){
            // Mysql数据同步
            seckillGoods.setStoreCount(0);
            // 库存不足后续处理:1.将该商品添加到Redis的布隆过滤波器,2.停止该商品的下单,3.去布隆过滤器中判断该商品是否在布隆过滤器中,3.如果在过滤器中则表明商品售罄
            seckillGoodsMapper.updateById(seckillGoods);
            // 删除redis缓存
            redisTemplate.boundHashOps("HotSeckillGoods").delete(id);
        }
        // 移除排队标识
//            redisTemplate.boundHashOps("HotSeckillGoods").delete(id);
        redisTemplate.delete("OrderQueue" + username);
        // 返回状态码
        return ADD_ORDER_OK;
    }
}
